<?php
/////////////////////////////////////////////////////////////////////////////
// 这个文件是 网城创想多用户商城 项目的一部分
//
// Copyright (c) 2007 - 2009 www.shopnc.net 
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : member_group.manage.php
 * ....用户组
 *
 * @copyright Copyright (c) 2007 - 2009 www.shopnc.net 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Tue May 26 17:30:30 CST 2009
 */

require_once("../global.inc.php");

class SysMemberGroupClass extends SystemFrameWork{
	/**
	 * 会员组对象
	 *
	 * @var obj
	 */
	var $obj_member_group;
	/**
	 * 验证对象
	 *
	 * @var obj
	 */
	var $obj_validate;
	
	function main(){
		/**
		 * 创建会员组对象
		 */
		if (!is_object($this->obj_member_group)){
			require_once ("member_group.class.php");
			$this->obj_member_group = new MemberGroupClass();
		}
		/**
		 * 创建验证对象
		 */
		if (!is_object($this->obj_validate)){
			require_once("commonvalidate.class.php");
			$this->obj_validate = new CommonValidate();
		}
		/**
		 * 语言包
		 */
		$this->getlang("sys_member_group.manage");
		
		switch ($this->_input['action']){
			case 'list':
				$this->_list();
				break;
			case 'ajax_list':
				$this->_ajax_list();
				break;
			case 'update':
				$this->_update();
				break;
			case 'set':
				$this->_set();
				break;
			case 'set_save':
				$this->_set_save();
				break;
			default:
				exit;
		}
		
	}
	
	/**
	 * 列表
	 */
	function _list(){
		$this->_ajax_list();
	}
	
	/**
	 * 
	 */
	function _ajax_list(){
		//会员组
		$condition['order_by'] = 'mg_score_lower asc';
		$group_array = $this->obj_member_group->getMemberGroupList($condition,$page);
		/**
		 * 页面输出
		 */
		$this->output('group_array',$group_array);
		$this->showpage('sys_member_group.list');
	}
	
	
	/**
	 * 更细用户组
	 */
	function _update(){
		//判断删除条目是否小于两条
		if (count($this->_input['mg_id'])-count($this->_input['del_id'])<=1){
			$this->redirectPath('error','',$this->_lang['errSysMgGroupNumberMustThanTwo'],1);
		}
		//验证是否有0
		if (!in_array('0',$this->_input['group_lower'])){
			$this->redirectPath('error','',$this->_lang['errSysMgLackZeroGroup'],1);
		}
		
		if (!empty($this->_input['del_id'])){
			foreach ($this->_input['del_id'] as $k => $v){
				$result = $this->obj_member_group->delMemberGroup($v);
				if ($result !== true){
					$this->redirectPath('error','',$this->_lang['errSysMgDelFail'],1);
				}else {
					unset($this->_input['mg_id'][$v]);
				}
			}
		}
		
		//取现有用户组内容
		$condition['order_by'] = 'mg_score_lower asc';
		$group_array = $this->obj_member_group->getMemberGroupList($condition,$page);
		$old_score = array();
		if (!empty($group_array)){
			foreach ($group_array as $k => $v){
				if (in_array($this->_input['group_lower'][$v['mg_id']],$old_score)){
					$this->redirectPath('error','',$this->_lang['errSysMgNowGroupSetIntervalIsWrong'],1);
				}
				if (!is_int(abs($this->_input['group_lower'][$v['mg_id']])) || !is_numeric($this->_input['group_lower'][$v['mg_id']])){
					$this->redirectPath('error','',$this->_lang['errSysMgNowGroupSetIsWrong'],1);
				}
				$old_score[$k] = $this->_input['group_lower'][$v['mg_id']];
				//更新信息
				$group_array[$k]['mg_name'] = $this->_input['group_name'][$v['mg_id']];
				$group_array[$k]['mg_score_lower'] = $this->_input['group_lower'][$v['mg_id']];
				$group_array[$k]['mg_stars'] = abs($this->_input['group_stars'][$v['mg_id']]);
			}
		}
		//判断新增的经验值是否存在，如果存在则返回错误
		if (!empty($this->_input['new_name'])){
			foreach ($this->_input['new_name'] as $k => $v){
				if (trim($this->_input['new_name'][$k]) == '' || trim($this->_input['new_lower'][$k]) == ''){
					unset($this->_input['new_name'][$k]);
				}else {
					if (in_array($this->_input['new_lower'][$k],$old_score)){
						$this->redirectPath('error','',$this->_lang['errSysMgNewGroupSetIntervalIsWrong'],1);
					}
					if (!is_numeric($this->_input['new_lower'][$k])){
						$this->redirectPath('error','',$this->_lang['errSysMgNewGroupSetIsWrong'],1);
					}
					$group_array[] = array(
						'mg_name'=>$this->_input['new_name'][$k],
						'mg_score_lower'=>$this->_input['new_lower'][$k],
						'mg_stars'=>abs($this->_input['new_stars'][$k])?abs($this->_input['new_stars'][$k]):0,
					);
					$old_score[] = $this->_input['new_lower'][$k];
				}
			}
		}
		
		//排序
//		sort($old_score);
		for ($i=0;$i<count($old_score);$i++)
		{
			for ($j=count($old_score)-2;$j>=$i;$j--)
			{
				if($old_score[$j+1]<$old_score[$j])
				{
					//重新排序积分
					$tmp = $old_score[$j+1];
					$old_score[$j+1]=$old_score[$j];
					$old_score[$j]=$tmp;
					
					//重新排序group数组
					$group_tmp = $group_array[$j+1];
					$group_array[$j+1]=$group_array[$j];
					$group_array[$j]=$group_tmp;
				}
			}
		}

		//新增和更新
		if (!empty($group_array)){
			foreach ($group_array as $k => $v){
				$value_array = array();
				$value_array['mg_name'] = $v['mg_name'];
				$value_array['mg_score_lower'] = isset($old_score[$k - 1]) ? $old_score[$k] : -999999999;
				$value_array['mg_score_higher'] = isset($old_score[$k + 1]) ? $old_score[$k + 1] : 999999999;
				$value_array['mg_stars'] = $v['mg_stars'];
				if ($v['mg_id'] == ''){//新增
					$this->obj_member_group->addMemberGroup($value_array);
				}else {//更新
					$value_array['mg_id'] = $v['mg_id'];
					$this->obj_member_group->updateMemberGroup($value_array);
				}
			}
		}
		//记录操作日志
		SystemPowerClass::addSysLog($this->_lang['langSysMgLogUpdate']);
		$this->redirectPath('error','',$this->_lang['langSysMgSetSucc'],1);
	}
	
	/**
	 * 设置用户组内容
	 */
	function _set(){
		$this->obj_validate->setValidate(array("input"=>$this->_input['id'],"require"=>"true","validator"=>"Number","message"=>$this->_lang['langSysCIDErr'])); 
		$error = $this->obj_validate->validate();
		if($error != ""){
			$this->redirectPath('error','',$error,1);
		}else{
			$group_array = $this->obj_member_group->getMemberGroupRow($this->_input['id']);
			/**
			 * 页面输出
			 */
			$this->output('group_array',$group_array);
			$this->showpage('sys_member_group.set');
		}
	}
	
	/**
	 * 保存设置
	 */
	function _set_save(){
		$this->obj_validate->setValidate(array("input"=>$this->_input['mg_id'],"require"=>"true","validator"=>"Number","message"=>$this->_lang['langSysCIDErr']));
		$this->obj_validate->setValidate(array("input"=>$this->_input['mg_name'],"require"=>"true","message"=>$this->_lang['errSysMgNameIsEmpty']));
		$this->obj_validate->setValidate(array("input"=>$this->_input['mg_sell_num'],"require"=>"true","validator"=>"Number","message"=>$this->_lang['errSysMgSellNumIsNotNumber']));
		$error = $this->obj_validate->validate();
		if($error != ""){
			$this->redirectPath('error','',$error,1);
		}else{
			$value_array = array();
			$value_array['mg_id'] = $this->_input['mg_id'];
			$value_array['mg_name'] = $this->_input['mg_name'];
			$value_array['mg_iflogin'] = $this->_input['mg_iflogin'];
			$value_array['mg_ifbuy'] = $this->_input['mg_ifbuy'];
			$value_array['mg_ifsell'] = $this->_input['mg_ifsell'];
			$value_array['mg_ifopen_shop'] = $this->_input['mg_ifopen_shop'];
			$value_array['mg_ifuse_credit'] = $this->_input['mg_ifuse_credit'];
			$value_array['mg_sell_num'] = $this->_input['mg_sell_num'];
			$result = $this->obj_member_group->updateMemberGroup($value_array);
			if ($result === true){
				/*记录操作日志*/
				SystemPowerClass::addSysLog($this->_lang['langSysMgLogSet']);
				$this->redirectPath('error','./member_group.manage.php?action=list',$this->_lang['langSysMgUpdateSucc'],1);
			}else {
				$this->redirectPath('error','',$this->_lang['errSysMgUpdateFail'],1);
			}
		}
	}
}
$member_group = new SysMemberGroupClass();
$member_group->main();
unset($member_group);
?>